package com.ynyyrj.school.controller;

import com.ynyyrj.school.pojo.*;
import com.ynyyrj.school.service.MajService;
import com.ynyyrj.school.service.SchListService;
import com.ynyyrj.school.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.Date;
import java.util.List;

@Controller
public class SchListController {

    @Autowired
    SchListService schListService;

    @Autowired
    StuService stuService;

    @Autowired
    MajService majService;

    @RequestMapping({"/dashboard","/"})
    public String toList(){
        return "dashboard";
    }

    @RequestMapping("/schoolList")
    public String SchoolList(Model model){
        Collection<SchoolList> lists=schListService.GetSchoolList();
        model.addAttribute("schools",lists);
        return "emp/list";
    }

    @RequestMapping("/cancel")
    public String cancel(){
        return "redirect:/schoolList";
    }

    @RequestMapping("/Add")
    public String toAdd(){
        return "emp/AddSch";
    }


    @RequestMapping("/saveSchool")
    public String add(String name){
        List<String> list1=schListService.GetName();
        boolean i=list1.contains(name);
        if(!i){
            Date date1=new Date();
            Date date2=new Date();
            school school=new school();
            school.setName(name);
            school.setTime_add(date1);
            school.setTime_update_lase(date2);
            schListService.addSchool(school);
            return "redirect:/schoolList";
        }
        return "redirect:/schoolList";
    }

    @RequestMapping("/updemp/{name}")
    public String update(@PathVariable("name") String name,Model model){
        model.addAttribute("s",name);
        int i=schListService.selectId(name);
        model.addAttribute("s1",i);
        int x=majService.Majnum(i);
        int y=stuService.Stunum(i);
        if(x+y==0){
            return "emp/UpdateSch";
        }
        return "redirect:/schoolList";
    }

    @RequestMapping("/UpdateSuccess/{aid}")
    public String update1(@PathVariable("aid") int aid,String name){
        List<String> list=schListService.GetName();
        boolean x=list.contains(name);
        if(!x){
            Date date1=new Date();
            school school=new school();
            school.setName(name);
            school.setAid(aid);
            school.setTime_update_lase(date1);
            schListService.updaSchool(school);
            return "redirect:/schoolList";
        }else {
            return "redirect:/schoolList";
        }
    }

    @RequestMapping("/delemp/{name}")
    public String delete(@PathVariable("name") String name,Model model){
        model.addAttribute("s",name);
        int i=schListService.selectId(name);
        int x=majService.Majnum(i);
        int y=stuService.Stunum(i);
        if(x+y==0){
            return "emp/DeleteSch";
        }
        return "redirect:/schoolList";
    }


    @RequestMapping("/DeleteSuccess/{name}")
    public String delete1( @PathVariable("name") String name){
        schListService.delSchool(name);
        return "redirect:/schoolList";
    }

    @RequestMapping("/listemp/{name}")
    public String tolist1(@PathVariable("name") String name,Model model){
        Collection<MajorList> lists=schListService.GetMajorList(name);
        model.addAttribute("majors",lists);
        model.addAttribute("s1",name);
        return "emp/list1";
    }

    @RequestMapping("/listemp/{name}/{name1}")
    public String tolist2(@PathVariable("name")String name1,@PathVariable("name1")String name2,Model model){
        Collection<StudentList> lists=schListService.GetStudentList(name1,name2);
        System.out.println(lists);
        model.addAttribute("students",lists);
        model.addAttribute("s",name1);
        model.addAttribute("s1",name2);
        return "emp/list2";
    }

    @RequestMapping("/addMaj/{name}")
    public String addMaj(@PathVariable("name") String name,Model model){
        model.addAttribute("s",name);
        return "emp/AddMaj";
    }

    @RequestMapping("/addMajSuccess/{name1}")
    public String addMajSuccess(@PathVariable("name1")String name1,String name){
        int i=schListService.selectId(name1);
        List<String> list=majService.GetName(i);
        boolean j=list.contains(name);
        if(!j){
            Date date1=new Date();
            Date date2=new Date();
            major major=new major();
            major.setName(name);
            major.setSchool_aid(i);
            major.setTime_add(date1);
            major.setTime_update_last(date2);
            System.out.println(major);
            majService.addMajor(major);
            return "redirect:/listemp/{name1}";
        }
        return "redirect:/listemp/{name1}";
    }



    @RequestMapping("/updMaj/{name}/{name1}")
    public String updaMaj(@PathVariable("name") String name,@PathVariable("name1")String name1, Model model){
        model.addAttribute("s",name1);
        int y=schListService.selectId(name);
        int i=majService.selectMid(name1,y);
        model.addAttribute("s1",i);
        model.addAttribute("s2",name);
        int x=stuService.stuMnum(i);
        if(x==0){
            return "emp/UpdateMaj";
        }
        return "redirect:/listemp/{name}";
    }

    @RequestMapping("/upMajSuccess/{name1}/{aid}")
    public String updMaj1(@PathVariable("name1")String name1,@PathVariable("aid")int aid,String name){
        int i=schListService.selectId(name1);
        List<String> list=majService.GetName(i);
        boolean j=list.contains(name);
        if(!j){
            Date date1=new Date();
            major major=new major();
            major.setAid(aid);
            major.setName(name);
            major.setSchool_aid(i);
            major.setTime_update_last(date1);
            majService.updaMajor(major);
            return "redirect:/listemp/{name1}";
        }
        return "redirect:/listemp/{name1}";
    }


    @RequestMapping("/delemp/{name}/{name1}")
    public String deleteMaj(@PathVariable("name")String name,@PathVariable("name1")String name1,Model model){
        int j=schListService.selectId(name);
        int x=majService.selectMid(name1,j);
        int i=stuService.stuMnum(x);
        if(i==0){
            model.addAttribute("s",name);
            model.addAttribute("s1",name1);
            return "emp/DeleteMaj";
        }
        return "redirect:/listemp/{name}";
    }

    @RequestMapping("/deleteMajSuccess/{name1}/{name}")
    public String deleteMajSuccess(@PathVariable("name1")String name1,@PathVariable("name")String name){
        int i=schListService.selectId(name1);
        majService.delMajor(name,i);
        return "redirect:/listemp/{name1}";
    }



}
